import java.util.LinkedList;
import java.util.Queue;

class MyStack {
    Queue<Integer> Queue1;
    Queue<Integer> Queue2;

    public MyStack() {
        Queue1 = new LinkedList<>();
        Queue2 = new LinkedList<>();

    }

    public void push(int x) {
        if(!Queue1.isEmpty()){
            Queue1.offer(x);
        }else if(!Queue2.isEmpty()){
            Queue2.offer(x);
        }else{
            Queue1.offer(x);
        }

    }

    public int pop() {
        if(empty()){
            return -1;
        }
        if(!Queue1.isEmpty()){
            for(int i = 0;i<Queue1.size()-1;i++){
                Queue2.offer(Queue1.poll());
            }
            return Queue1.poll();
        }else{
            for(int i = 0;i<Queue2.size()-1;i++){
                Queue1.offer(Queue2.poll());
            }
            return Queue2.poll();
        }
    }

    public int top() {
        if(empty()){
            return -1;
        }
        if(!Queue1.isEmpty()){
            for(int i = 0;i<Queue1.size()-1;i++){
                Queue2.offer(Queue1.poll());
            }
            int k = Queue1.peek();
            Queue2.offer(Queue1.poll());
            return k;
        }else{
            for(int i = 0;i<Queue2.size()-1;i++){
                Queue1.offer(Queue2.poll());
            }
            int k = Queue2.peek();
            Queue1.offer(Queue2.poll());
            return k;
        }

    }

    public boolean empty() {
        return Queue1.isEmpty()&&Queue2.isEmpty();
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */